<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta http-equiv="Content-Type"content="text/html; charset=utf-8">
        <title></title>
    </head>
    <body>
    <?php
        
        function pprint($info){
            echo '<p>'.$info.'</p>';
        }


        pprint('先输出点东西看看aabbcc112233');
        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = '123456';
        
        // 连接sql
        $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
       // mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary",$conn);
        if(! $conn )
        {
          die('连接失败: ' . mysql_error());
        }
        pprint( 'Connected successfully');
        
        // 选择指定的数据库
        $select = mysqli_select_db( $conn,'test_db' );
        // 检查是否数据库存在
        if(!$select){
            // 创建数据库
            $sql = 'CREATE Database test_db';
            $retval = mysqli_query($conn, $sql);
            
            if(! $retval )
            {
              die('创建数据库失败: ' . mysql_error());
            }
        }else{
            pprint('数据库 test_db: 已经存在');
        }
        // 创建表
        $table = 'employee';
        
        $result = mysqli_query($conn, "select * from employee");
        if(!$result){
             echo 'xxxx:  不存在';
            $sql = 'CREATE TABLE employee( '.
           'emp_id INT NOT NULL AUTO_INCREMENT, '.
           'emp_name VARCHAR(20) NOT NULL, '.
           'emp_address  VARCHAR(20) NOT NULL, '.
           'emp_salary   INT NOT NULL, '.
           'join_date    timestamp(6) NOT NULL, '.
           'primary key ( emp_id ))';

            $retval = mysqli_query( $conn, $sql );
            if(! $retval )
            {
              die('创建 表失败: ' . mysqli_error( $conn));
            }
             pprint('表 employee: 创建完成');
        }else{
             pprint('表 employee: 已经存在');
        }
        
        
        // 插入数据
         $sql = 'INSERT INTO employee '.
           '(emp_name,emp_address, emp_salary, join_date) '.
           'VALUES ( "guest", "XYZ", 2000, NOW() )';
        $retval = mysqli_query(  $conn, $sql );
        if(! $retval )
        {
          die('Could not enter data: ' . mysql_error());
        }

        
        // 读取数据
         $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee';
         $retval = mysqli_query( $conn, $sql );
        if(! $retval )
        {
          die('Could not get data: ' . mysql_error());
        }
        while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
        {
            echo "EMP ID :{$row['emp_id']}  <br> ".
                 "EMP NAME : {$row['emp_name']} <br> ".
                 "EMP SALARY : {$row['emp_salary']} <br> ".
                 "--------------------------------<br>";
        } 
        
        $emp_salary = 100;
        $emp_id = 1;
        // 更新数据
        $sql = "UPDATE employee ".
           "SET emp_salary = $emp_salary ".
           "WHERE emp_id = $emp_id" ;
         $retval = mysqli_query( $conn, $sql );
        if(! $retval )
        {
          die('Could not update data: ' . mysqli_error( $conn));
        }
        
        $emp_id = 3;
        // 删除数据
        $sql = "DELETE FROM  employee ".
           "WHERE emp_id = $emp_id" ;
        $retval = mysqli_query( $conn, $sql );
        if(! $retval )
        {
          die('Could not delete data: ' . mysqli_error( $conn));
        }
        
        
        echo '<p>============</p>';
        
         // 读取数据
         $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee';
         $retval = mysqli_query( $conn, $sql );
        if(! $retval )
        {
          die('Could not get data: ' . mysqli_error( $conn));
        }
        while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
        {
            echo "EMP ID :{$row['emp_id']}  <br> ".
                 "EMP NAME : {$row['emp_name']} <br> ".
                 "EMP SALARY : {$row['emp_salary']} <br> ".
                 "--------------------------------<br>";
        } 
        
        
        
        
        mysqli_close($conn);
    ?>
    </body>
</html>
